/*******************************************************************************
* Long-term effects of weather-induced migration on urban labor and housing 
  markets
* Busso & Chauvin

* Purpose: Format final tables and convert to .tex format. 

*******************************************************************************/

/*******************************************************************************
1. General formatting
*******************************************************************************/
global outcomes_1 d_l_wageres2_r_9110 d_l_emp_r_9110 d_l_wageres2_r_isv_9110 ///
d_l_emp_r_isv_9110 d_l_wageres2_r_imf_9110 d_l_emp_r_imf_9110 ///
d_l_wageres2_r_nohs_9110 d_l_emp_r_nohs_9110 d_l_wageres2_r_hs_9110 ///
d_l_emp_r_hs_9110 d_l_wageres2_g_9110 d_l_emp_g_9110 d_rate_part_r_9110 ///
d_isv_sh_r_9110 d_imf_sh_r_9110 d_hs_r_9110 d_rate_inform_r_9110 ///
d_rate_inform2_r_9110 

global outcomes_2 d_l_rentres2_r_9110 d_l_houses_r_9110 d_l_rooms_r_9110 ///
d_l_rentres2_l_r_9110 d_l_houses_l_r_9110 d_l_rooms_l_r_9110 ///
d_l_rentres2_m_r_9110 d_l_houses_m_r_9110 d_l_rooms_m_r_9110 ///
d_l_rentres2_h_r_9110 d_l_houses_h_r_9110 d_l_rooms_h_r_9110 ///
d_l_rentres2_g_9110 d_l_houses_g_9110 d_l_rooms_g_9110 ///
d_own_all_9110 d_own_residents_9110 d_own_all_l_9110 d_own_residents_l_9110 ///
d_own_all_m_9110 d_own_residents_m_9110 d_own_all_h_9110 d_own_residents_h_9110 ///
d_l_rentres2_9110 d_l_houses_9110 d_l_rooms_9110 ///
d_l_rentres2_l_9110 d_l_houses_l_9110 d_l_rooms_l_9110 ///
d_l_rentres2_m_9110 d_l_houses_m_9110 d_l_rooms_m_9110 ///
d_l_rentres2_h_9110 d_l_houses_h_9110 d_l_rooms_h_9110 ///
d_l_rentres2_l_g_9110 d_l_houses_l_g_9110 d_l_rooms_l_g_9110 ///
d_l_rentres2_m_g_9110 d_l_houses_m_g_9110 d_l_rooms_m_g_9110 ///
d_l_rentres2_h_g_9110 d_l_houses_h_g_9110 d_l_rooms_h_g_9110 

global outcomes_3 d_cityfe_l_wage_r_9110 d_cityfe_l_wage_r_isv_9110 ///
d_cityfe_l_wage_r_imf_9110 d_cityfe_l_wage_r_nohs_9110 d_cityfe_l_wage_r_hs_9110 ///
d_cityfe_l_wage_g_9110 d_cityfe_l_rent_r_9110 d_cityfe_l_rent_l_rr_9110 ///
d_cityfe_l_rent_m_rr_9110 d_cityfe_l_rent_h_rr_9110 d_cityfe_l_rent_g_9110

global outcomes_4 d_l_wageres2_r_0010 d_l_emp_r_0010 d_l_wageres2_r_isv_0010 ///
d_l_emp_r_isv_0010 d_l_wageres2_r_imf_0010 d_l_emp_r_imf_0010 ///
d_l_wageres2_r_nohs_0010 d_l_emp_r_nohs_0010 d_l_wageres2_r_hs_0010 ///
d_l_emp_r_hs_0010 d_l_wageres2_g_0010 d_l_emp_g_0010

forval j = 1/4 {

	local contador = 0

	foreach x in ${outcomes_`j'} {
	
		local contador = `contador' + 1

		* 1. OLS results
		** 1.1 Load results and generate variable with outcome name
		estimates use "$da_tmp/`x'.ster"
		ereturn disp
		mat t0 = r(table)'
		clear
		svmat2 t0, rnames(var) names(col)

		gen outcome = "`x'"
		
		keep if strpos(var, "rate_mig_0110") > 0
		
		** 1.2 Significance level
		gen S = "*" if pvalue < 0.1
		replace S = "**" if pvalue < 0.05
		replace S = "***" if pvalue < 0.01

		** 1.3 Put se underneath coeffs 
		preserve
		gen OLS = string(b, "%20.3fc")
		replace OLS = OLS + S
		gen order_no = 1
		keep outcome order_no OLS
		tempfile means
		save `means', replace
		restore
		
		keep se outcome 
		gen OLS = string(se, "%20.3fc")	
		replace OLS = "(" + OLS + ")"
		drop se
		gen order_no = 2
		
		append using `means'	
		sort order_no
		order outcome order_no
		
		tempfile ols
		save `ols', replace
		
		* 2. IV results
		forval r = 1/6 {
			estimates use "$da_tmp/`x'_`r'.ster"
			ereturn disp
			mat t0 = r(table)'
			clear
			svmat2 t0, rnames(var) names(col)
			
			gen outcome = "`x'"
			
			keep if (strpos(var, "rate_mig_0110") > 0) 
			
			gen S = "*" if pvalue < 0.1
			replace S = "**" if pvalue < 0.05
			replace S = "***" if pvalue < 0.01

			* Put se underneath coeffs 
			preserve
			gen IV`r' = string(b, "%20.3fc")
			replace IV`r' = IV`r' + S
			gen order_no = 1
			keep outcome order_no IV`r'
			tempfile means
			save `means', replace
			restore
			
			keep se outcome 
			gen IV`r' = string(se, "%20.3fc")	
			replace IV`r' = "(" + IV`r' + ")"
			drop se
			gen order_no = 2
			
			append using `means'	
			sort order_no
			order outcome order_no
			
			merge 1:1 outcome order_no using `ols'
			drop _merge
			save `ols', replace 

		} // closes loop that goes through IV specifications
		
		gen order_outcomes = `contador'
		
		order outcome order_outcomes order_no OLS IV1 IV2 IV3 IV4 IV5 IV6
		
		sleep 1000
		
		* 4. Append the outcomes of the same group
		if `contador' == 1 {
			save "$da_out/results_`j'.dta", replace
		}
		
		else {
			append using "$da_out/results_`j'.dta"
			sort order_outcomes order_no
			save "$da_out/results_`j'.dta", replace	
		}
		
	} // closes loop that goes through outcomes of the same group
	
} // closes loop that goes through groups

/*******************************************************************************
2. SPEI absolute value
*******************************************************************************/
global outcomes_1 d_l_wageres2_r_9110 d_l_emp_r_9110 d_l_wageres2_r_isv_9110 ///
d_l_emp_r_isv_9110 d_l_wageres2_r_imf_9110 d_l_emp_r_imf_9110 ///
d_l_wageres2_r_nohs_9110 d_l_emp_r_nohs_9110 d_l_wageres2_r_hs_9110 ///
d_l_emp_r_hs_9110 d_l_wageres2_g_9110 d_l_emp_g_9110

global outcomes_2 d_l_rentres2_r_9110 d_l_houses_r_9110 d_l_rooms_r_9110 ///
d_l_rentres2_l_r_9110 d_l_houses_l_r_9110 d_l_rooms_l_r_9110 ///
d_l_rentres2_m_r_9110 d_l_houses_m_r_9110 d_l_rooms_m_r_9110 ///
d_l_rentres2_h_r_9110 d_l_houses_h_r_9110 d_l_rooms_h_r_9110 ///
d_l_rentres2_g_9110 d_l_houses_g_9110 d_l_rooms_g_9110 

forval j = 1/2 {

	local contador = 0

	foreach x in ${outcomes_`j'} {
	
		local contador = `contador' + 1

		* 1. OLS results
		** 1.1 Load results and generate variable with outcome name
		estimates use "$da_tmp/`x'_w.ster"
		ereturn disp
		mat t0 = r(table)'
		clear
		svmat2 t0, rnames(var) names(col)

		gen outcome = "`x'"
		
		keep if strpos(var, "rate_mig_0110") > 0
		
		** 1.2 Significance level
		gen S = "*" if pvalue < 0.1
		replace S = "**" if pvalue < 0.05
		replace S = "***" if pvalue < 0.01

		** 1.3 Put se underneath coeffs 
		preserve
		gen OLS = string(b, "%20.3fc")
		replace OLS = OLS + S
		gen order_no = 1
		keep outcome order_no OLS
		tempfile means
		save `means', replace
		restore
		
		keep se outcome 
		gen OLS = string(se, "%20.3fc")	
		replace OLS = "(" + OLS + ")"
		drop se
		gen order_no = 2
		
		append using `means'	
		sort order_no
		order outcome order_no
		
		tempfile ols
		save `ols', replace
		
		* 2. IV results
		forval r = 1/6 {
			estimates use "$da_tmp/`x'_`r'_w.ster"
			ereturn disp
			mat t0 = r(table)'
			clear
			svmat2 t0, rnames(var) names(col)
			
			gen outcome = "`x'"
			
			keep if (strpos(var, "rate_mig_0110") > 0) 
			
			gen S = "*" if pvalue < 0.1
			replace S = "**" if pvalue < 0.05
			replace S = "***" if pvalue < 0.01

			* Put se underneath coeffs 
			preserve
			gen IV`r' = string(b, "%20.3fc")
			replace IV`r' = IV`r' + S
			gen order_no = 1
			keep outcome order_no IV`r'
			tempfile means
			save `means', replace
			restore
			
			keep se outcome 
			gen IV`r' = string(se, "%20.3fc")	
			replace IV`r' = "(" + IV`r' + ")"
			drop se
			gen order_no = 2
			
			append using `means'	
			sort order_no
			order outcome order_no
			
			merge 1:1 outcome order_no using `ols'
			drop _merge
			save `ols', replace 

		} // closes loop that goes through IV specifications
		
		gen order_outcomes = `contador'
		
		order outcome order_outcomes order_no OLS IV1 IV2 IV3 IV4 IV5 IV6
		
		sleep 1000
		
		* 4. Append the outcomes of the same group
		if `contador' == 1 {
			save "$da_out/results_`j'_w.dta", replace
		}
		
		else {
			append using "$da_out/results_`j'_w.dta"
			sort order_outcomes order_no
			save "$da_out/results_`j'_w.dta", replace	
		}
		
	} // closes loop that goes through outcomes of the same group
	
} // closes loop that goes through groups

/*******************************************************************************
3. Clustering at the mesoregion level
*******************************************************************************/
global outcomes_1 d_l_wageres2_r_9110 d_l_emp_r_9110 d_l_wageres2_r_isv_9110 ///
d_l_emp_r_isv_9110 d_l_wageres2_r_imf_9110 d_l_emp_r_imf_9110 ///
d_l_wageres2_r_nohs_9110 d_l_emp_r_nohs_9110 d_l_wageres2_r_hs_9110 ///
d_l_emp_r_hs_9110 d_l_wageres2_g_9110 d_l_emp_g_9110

global outcomes_2 d_l_rentres2_r_9110 d_l_houses_r_9110 d_l_rooms_r_9110 ///
d_l_rentres2_l_r_9110 d_l_houses_l_r_9110 d_l_rooms_l_r_9110 ///
d_l_rentres2_m_r_9110 d_l_houses_m_r_9110 d_l_rooms_m_r_9110 ///
d_l_rentres2_h_r_9110 d_l_houses_h_r_9110 d_l_rooms_h_r_9110 ///
d_l_rentres2_g_9110 d_l_houses_g_9110 d_l_rooms_g_9110 

forval j = 1/2 {

	local contador = 0

	foreach x in ${outcomes_`j'} {
	
		local contador = `contador' + 1

		* 1. OLS results
		** 1.1 Load results and generate variable with outcome name
		estimates use "$da_tmp/`x'_meso.ster"
		ereturn disp
		mat t0 = r(table)'
		clear
		svmat2 t0, rnames(var) names(col)

		gen outcome = "`x'"
		
		keep if strpos(var, "rate_mig_0110") > 0
		
		** 1.2 Significance level
		gen S = "*" if pvalue < 0.1
		replace S = "**" if pvalue < 0.05
		replace S = "***" if pvalue < 0.01

		** 1.3 Put se underneath coeffs 
		preserve
		gen OLS = string(b, "%20.3fc")
		replace OLS = OLS + S
		gen order_no = 1
		keep outcome order_no OLS
		tempfile means
		save `means', replace
		restore
		
		keep se outcome 
		gen OLS = string(se, "%20.3fc")	
		replace OLS = "(" + OLS + ")"
		drop se
		gen order_no = 2
		
		append using `means'	
		sort order_no
		order outcome order_no
		
		tempfile ols
		save `ols', replace
		
		* 2. IV results
		forval r = 1/6 {
			estimates use "$da_tmp/`x'_`r'_meso.ster"
			ereturn disp
			mat t0 = r(table)'
			clear
			svmat2 t0, rnames(var) names(col)
			
			gen outcome = "`x'"
			
			keep if (strpos(var, "rate_mig_0110") > 0) 
			
			gen S = "*" if pvalue < 0.1
			replace S = "**" if pvalue < 0.05
			replace S = "***" if pvalue < 0.01

			* Put se underneath coeffs 
			preserve
			gen IV`r' = string(b, "%20.3fc")
			replace IV`r' = IV`r' + S
			gen order_no = 1
			keep outcome order_no IV`r'
			tempfile means
			save `means', replace
			restore
			
			keep se outcome 
			gen IV`r' = string(se, "%20.3fc")	
			replace IV`r' = "(" + IV`r' + ")"
			drop se
			gen order_no = 2
			
			append using `means'	
			sort order_no
			order outcome order_no
			
			merge 1:1 outcome order_no using `ols'
			drop _merge
			save `ols', replace 

		} // closes loop that goes through IV specifications
		
		gen order_outcomes = `contador'
		
		order outcome order_outcomes order_no OLS IV1 IV2 IV3 IV4 IV5 IV6 
		
		sleep 1000
		
		* 4. Append the outcomes of the same group
		if `contador' == 1 {
			save "$da_out/results_`j'_meso.dta", replace
		}
		
		else {
			append using "$da_out/results_`j'_meso.dta"
			sort order_outcomes order_no
			save "$da_out/results_`j'_meso.dta", replace	
		}
		
	} // closes loop that goes through outcomes of the same group
	
} // closes loop that goes through groups

/*******************************************************************************
4. Control of commuting by housing quality
*******************************************************************************/
global outcomes_1 d_l_rentres2_l_r_9110 d_l_houses_l_r_9110 d_l_rooms_l_r_9110 ///
d_l_rentres2_m_r_9110 d_l_houses_m_r_9110 d_l_rooms_m_r_9110 ///
d_l_rentres2_h_r_9110 d_l_houses_h_r_9110 d_l_rooms_h_r_9110

local contador = 0

foreach x in $outcomes_1 {
	
	local contador = `contador' + 1
	
	* 1. OLS results
	estimates use "$da_tmp/`x'_com.ster"
	ereturn disp
	mat t0 = r(table)'
	clear
	svmat2 t0, rnames(var) names(col)

	gen outcome = "`x'"
	
	keep if strpos(var, "rate_mig_0110") > 0
	
	** 1.2 Significance level
	gen S = "*" if pvalue < 0.1
	replace S = "**" if pvalue < 0.05
	replace S = "***" if pvalue < 0.01

	** 1.3 Put se underneath coeffs 
	preserve
	gen OLS = string(b, "%20.3fc")
	replace OLS = OLS + S
	gen order_no = 1
	keep outcome order_no OLS
	tempfile means
	save `means', replace
	restore
	
	keep se outcome 
	gen OLS = string(se, "%20.3fc")	
	replace OLS = "(" + OLS + ")"
	drop se
	gen order_no = 2
	
	append using `means'	
	sort order_no
	order outcome order_no
	
	tempfile ols
	save `ols', replace

	* 2. IV results
	forval r = 7/9 {	
		
		estimates use "$da_tmp/`x'_`r'_com.ster"
		ereturn disp
		mat t0 = r(table)'
		clear
		svmat2 t0, rnames(var) names(col)
		
		gen outcome = "`x'"
		
		keep if (strpos(var, "rate_mig_0110") > 0) 
		
		gen S = "*" if pvalue < 0.1
		replace S = "**" if pvalue < 0.05
		replace S = "***" if pvalue < 0.01

		* Put se underneath coeffs 
		preserve
		gen IV`r' = string(b, "%20.3fc")
		replace IV`r' = IV`r' + S
		gen order_no = 1
		keep outcome order_no IV`r'
		tempfile means
		save `means', replace
		restore
		
		keep se outcome 
		gen IV`r' = string(se, "%20.3fc")	
		replace IV`r' = "(" + IV`r' + ")"
		drop se
		gen order_no = 2
		
		append using `means'	
		sort order_no
		order outcome order_no
		
		merge 1:1 outcome order_no using `ols'
		drop _merge
		save `ols', replace 

	} // closes loop that goes through IV specifications
	
	gen order_outcomes = `contador'
	
	order outcome order_outcomes order_no OLS IV7 IV8 IV9
	
	sleep 1000
	
	* 4. Append the outcomes of the same group
	if `contador' == 1 {
		
		save "$da_tmp/results_comtime.dta", replace
		
	}
	
	else {
		
		append using "$da_tmp/results_comtime.dta"
		sort order_outcomes order_no
		save "$da_tmp/results_comtime.dta", replace	
		
	}
	
} // closes loop that goes through outcomes of the same group
		
drop OLS

gen quality = 1
replace quality = 2 if strpos(outcome, "_m_") > 0
replace quality = 3 if strpos(outcome, "_h_") > 0

gen IV = IV7
replace IV = IV8 if quality == 2
replace IV = IV9 if quality == 3

drop IV7 IV8 IV9 order_outcomes order_no quality
rename IV IV_aux

gen order_new = _n

save "$da_tmp/results_comtime.dta", replace

/*******************************************************************************
5. Emigration effect
*******************************************************************************/
global outcomes_1 expulsion_0010 

local contador = 0

foreach x in $outcomes_1 {
	
	local contador = `contador' + 1
	
	* 1. OLS results
	** 1.1 Load results and generate variable with outcome name
	estimates use "$da_tmp/`x'_ols.ster"
	ereturn disp
	mat t0 = r(table)'
	clear
	svmat2 t0, rnames(var) names(col)

	gen outcome = "`x'"

	keep if strpos(var, "rate_mig_") > 0

	** 1.2 Significance level
	gen S = "*" if pvalue < 0.1
	replace S = "**" if pvalue < 0.05
	replace S = "***" if pvalue < 0.01

	** 1.3 Put se underneath coeffs 
	preserve
	gen OLS = string(b, "%20.3fc")
	replace OLS = OLS + S
	gen order_no = 1
	keep outcome order_no OLS
	tempfile means
	save `means', replace
	restore

	keep se outcome 
	gen OLS = string(se, "%20.3fc")	
	replace OLS = "(" + OLS + ")"
	drop se
	gen order_no = 2

	append using `means'	
	sort order_no
	order outcome order_no

	tempfile ols
	save `ols', replace

	* 2. IV results
	forval r = 1/6 {
		estimates use "$da_tmp/`x'_`r'.ster"
		ereturn disp
		mat t0 = r(table)'
		clear
		svmat2 t0, rnames(var) names(col)
		
		gen outcome = "`x'"
		
		keep if (strpos(var, "rate_mig_") > 0) 
		
		gen S = "*" if pvalue < 0.1
		replace S = "**" if pvalue < 0.05
		replace S = "***" if pvalue < 0.01

		* Put se underneath coeffs 
		preserve
		gen IV`r' = string(b, "%20.3fc")
		replace IV`r' = IV`r' + S
		gen order_no = 1
		keep outcome order_no IV`r'
		tempfile means
		save `means', replace
		restore
		
		keep se outcome 
		gen IV`r' = string(se, "%20.3fc")	
		replace IV`r' = "(" + IV`r' + ")"
		drop se
		gen order_no = 2
		
		append using `means'	
		sort order_no
		order outcome order_no
		
		merge 1:1 outcome order_no using `ols'
		drop _merge
		save `ols', replace 

	} // closes loop that goes through IV specifications
	
	gen order_outcomes = `contador'
	
	order outcome order_outcomes order_no OLS IV1 IV2 IV3 IV4 IV5 IV6
	
	sleep 1000
	
	* 4. Append the outcomes of the same group
	if `contador' == 1 {
		save "$da_tmp/emigration_effect.dta", replace
	}
	
	else {
		append using "$da_tmp/emigration_effect.dta"
		sort order_outcomes order_no
		save "$da_tmp/emigration_effect.dta", replace	
	}

} // closes loop that goes through outcomes of the same group

/*******************************************************************************
6. Specific formatting
*******************************************************************************/
/* Table 2: Effects of Weather Shocks on Migration
*******************************************************************************/
use "$da_tmp/first_stage_rural.dta", replace
append using "$da_tmp/first_stage_immig.dta"

drop var

gen s1 = "*" if c2 < 0.1
replace s1 = "**" if c2 < 0.05
replace s1 = "***" if c2 < 0.01
gen coef1 = string(c1, "%20.3fc")
replace coef1 = string(c1, "%20.0fc") if _n == 3 | _n == 8
replace coef1 = coef1 + s1 if _n == 1 | _n == 5
replace coef1 = "(" + coef1 + ")" if _n == 2 | _n == 6
replace coef1 = string(c1, "%20.2fc") if _n == 7

gen s2 = "*" if c4 < 0.1
replace s2 = "**" if c4 < 0.05
replace s2 = "***" if c4 < 0.01
gen coef2 = string(c3, "%20.3fc")
replace coef2 = string(c3, "%20.0fc") if _n == 3 | _n == 8
replace coef2 = coef2 + s2 if _n == 1 | _n == 5
replace coef2 = "(" + coef2 + ")" if _n == 2 | _n == 6
replace coef2 = string(c3, "%20.2fc") if _n == 7

gen s3 = "*" if c6 < 0.1
replace s3 = "**" if c6 < 0.05
replace s3 = "***" if c6 < 0.01
gen coef3 = string(c5, "%20.3fc")
replace coef3 = string(c5, "%20.0fc") if _n == 3 | _n == 8
replace coef3 = coef3 + s3 if _n == 1 | _n == 5
replace coef3 = "(" + coef3 + ")" if _n == 2 | _n == 6
replace coef3 = string(c5, "%20.2fc") if _n == 7

keep coef*

gen outcome = ""
replace outcome = "Observations" if _n == 3 | _n == 8
replace outcome = "Average of dependent variable" if _n == 4 | _n == 9
replace outcome = "F statistic" if _n == 7
replace outcome = "\cmidrule{2-4}Average $-1 \times SPEI$" if _n == 1
replace outcome = "\cmidrule{2-4}SPEI-based shift-share IV" if _n == 5
order outcome

insobs 1, before(5)
insobs 1, before(3)
insobs 1, before(9)

egen latex = concat(*), p("&")
replace latex = latex + "\\"
keep latex

insobs 4, before(1)
replace latex = "\begin{tabular}{lccc}" in 1
replace latex = "\toprule & (1) & \multicolumn{1}{c}{(2)} & \multicolumn{1}{c}{(3)} \\" in 2
replace latex = "\multicolumn{4}{l}{\textbf{Panel A: Effects of precipitations on rural emigration }} \\" in 3
replace latex = "& \multicolumn{3}{c}{\textit{Emigration rate from rural areas}} \\" in 4
insobs 6, before(11)
replace latex = "Macrorregion fixed effects & Yes & \multicolumn{1}{c}{Yes} & \multicolumn{1}{c}{Yes} \\" in 11
replace latex = "Main controls set & No & \multicolumn{1}{c}{Yes} & \multicolumn{1}{c}{Yes} \\" in 12
replace latex = "Log population control & No & \multicolumn{1}{c}{No} & \multicolumn{1}{c}{Yes} \\" in 13
replace latex = "&&& \\" in 14
replace latex = "\multicolumn{4}{l}{\textbf{Panel B: Effect of weather-based IV on rural immigration }} \\" in 15
replace latex = "& \multicolumn{3}{c}{\textit{Rural immigration rate to cities}} \\" in 16
insobs 6, after(22)
replace latex = "&&& \\" in 23
replace latex = "Macrorregion fixed effects & Yes & \multicolumn{1}{c}{Yes} & \multicolumn{1}{c}{Yes} \\" in 24
replace latex = "Main controls set & No & \multicolumn{1}{c}{Yes} & \multicolumn{1}{c}{Yes} \\" in 25
replace latex = "Log population control & No & \multicolumn{1}{c}{No} & \multicolumn{1}{c}{Yes} \\" in 26
replace latex = "\bottomrule & & & \\" in 27
replace latex = "\end{tabular}" in 28

cap file close myfile
file open myfile using "$da_out/tables/table_2.tex", write replace

forv i = 1/`=_N'{
	loc t0 = latex in `i'
	file write myfile `"`t0'"' _n 
} 

file close myfile

/* Table 3: Labor Market - Main Table
*******************************************************************************/
use "$da_out/results_1.dta", replace

drop order_outcomes order_no

keep if strpos(outcome, "wageres2_r_9110") > 0 | strpos(outcome, "emp_r_9110") > 0

forvalues i = 2(2)`=_N' {
	replace outcome = "" in `i'
}

keep outcome OLS IV1 IV2 IV3

replace outcome = "\cmidrule{2-5} $\Delta$ Mean Log Wages" if strpos(outcome, "wageres2_r_9110") > 0
replace outcome = "$\Delta$ Log Total Employment" if strpos(outcome, "emp_r_9110") > 0

egen latex = concat(*), p("&")
replace latex = latex + "\\"
keep latex

insobs 2, before(1)
replace latex = "\toprule & OLS & IV & IV & IV \\" in 1
replace latex = "& (1) & (2) & (3) & (4) \\" in 2

insobs 5, after(6)
replace latex = "\midrule Macroregion fixed effects & Yes & Yes & Yes & Yes \\" in 7
replace latex = "Main controls set & Yes & No & Yes & Yes \\" in 8
replace latex = "Log population control & Yes & No & No & Yes \\" in 9
replace latex = "\bottomrule & & & & \\" in 10
replace latex = "\end{tabular}" in 11

insobs 1, before(1)
replace latex = "\begin{tabular}{lcccc}" in 1

cap file close myfile
file open myfile using "$da_out/tables/table_3.tex", write replace

forv i = 1/`=_N'{
	loc t0 = latex in `i'
	file write myfile `"`t0'"' _n 
} 

file close myfile

/* Table 4: Labor Market - Main Table - Heterogeneity
*******************************************************************************/
use "$da_out/results_1.dta", replace

drop if strpos(outcome, "wageres2_r_9110") > 0 | ///
strpos(outcome, "emp_r_9110") > 0 | strpos(outcome, "part") > 0 | ///
strpos(outcome, "sh") > 0 | strpos(outcome, "d_hs") > 0 | ///
strpos(outcome, "inform") > 0 

keep outcome OLS IV1 IV2 IV3 order_no order_outcomes

sort order_outcomes order_no

replace outcome = "~~~~~$\Delta$ Mean Log Wages" if strpos(outcome, "wageres2_r") > 0
replace outcome = "~~~~~$\Delta$ Log Total Employment" if strpos(outcome, "emp_r") > 0
replace outcome = "$\Delta$ Mean Log Wages" if strpos(outcome, "wageres2_g") > 0
replace outcome = "$\Delta$ Log Total Employment" if strpos(outcome, "emp_g") > 0

drop order_outcomes order_no 

forvalues i = 2(2)`=_N' {
	replace outcome = "" in `i'
}

insobs 1, before(1)
insobs 1, before(10)
insobs 1, before(19)
replace outcome = "\cmidrule{2-5} \textbf{Panel A: By industry}" in 1
replace outcome = "\textbf{Panel B: By schooling attainment}" in 10
replace outcome = "\textbf{Panel C: Among recent migrants}" in 19

insobs 1, before(2)
insobs 1, before(7)
insobs 1, before(13)
insobs 1, before(18)
replace outcome = "\textit{Services}" in 2
replace outcome = "\textit{Manufacturing}" in 7
replace outcome = "\textit{Less than high school}" in 13
replace outcome = "\textit{High school or more}" in 18

egen latex = concat(*), p("&")
replace latex = latex + "\\"
keep latex

insobs 2, before(1)
replace latex = "\toprule & OLS & IV & IV & IV \\" in 1
replace latex = "& (1) & (2) & (3) & (4) \\" in 2

insobs 5, after(29)
replace latex = "\midrule Macroregion fixed effects & Yes & Yes & Yes & Yes \\" in 30
replace latex = "Main controls set & Yes & No & Yes & Yes \\" in 31
replace latex = "Log population control & Yes & No & No & Yes \\" in 32
replace latex = "\bottomrule & & & & \\" in 33
replace latex = "\end{tabular}" in 34

insobs 1, before(1)
replace latex = "\begin{tabular}{lcccc}" in 1

cap file close myfile
file open myfile using "$da_out/tables/table_4.tex", write replace

forv i = 1/`=_N'{
	loc t0 = latex in `i'
	file write myfile `"`t0'"' _n 
} 

file close myfile

/* Table 5: Housing Market - Main Table 
*******************************************************************************/
use "$da_out/results_2.dta", replace

keep if strpos(outcome, "rentres2_r_9110") > 0 | strpos(outcome, "houses_r_9110") > 0 | ///
strpos(outcome, "rooms_r_9110") > 0

drop order_outcomes order_no

forvalues i = 2(2)`=_N' {
	replace outcome = "" in `i'
}

keep outcome OLS IV1 IV2 IV3

replace outcome = "\cmidrule{2-5} $\Delta$ Mean Log Housing Rents" if strpos(outcome, "rentres") > 0
replace outcome = "$\Delta$ Log Total Number of Housing Units" if strpos(outcome, "houses") > 0
replace outcome = "$\Delta$ Log Total Number of Rooms" if strpos(outcome, "rooms") > 0

egen latex = concat(*), p("&")
replace latex = latex + "\\"
keep latex

insobs 2, before(1)
replace latex = "\toprule & OLS & IV & IV & IV \\" in 1
replace latex = "& (1) & (2) & (3) & (4) \\" in 2

insobs 5, after(8)
replace latex = "\midrule Macroregion fixed effects & Yes & Yes & Yes & Yes \\" in 9
replace latex = "Main controls set & Yes & No & Yes & Yes \\" in 10
replace latex = "Log population control & Yes & No & No & Yes \\" in 11
replace latex = "\bottomrule & & & & \\" in 12
replace latex = "\end{tabular}" in 13

insobs 1, before(1)
replace latex = "\begin{tabular}{lcccc}" in 1

cap file close myfile
file open myfile using "$da_out/tables/table_5.tex", write replace

forv i = 1/`=_N'{
	loc t0 = latex in `i'
	file write myfile `"`t0'"' _n 
} 

file close myfile

/* Table 6: Housing Market - Main Table - Heterogeneity
*******************************************************************************/
use "$da_out/results_2.dta", replace

keep if strpos(outcome, "l_r_9110") > 0 | strpos(outcome, "m_r_9110") > 0 | ///
strpos(outcome, "h_r_9110") > 0 | strpos(outcome, "rentres2_g_9110") > 0 | ///
strpos(outcome, "houses_g_9110") > 0 | strpos(outcome, "rooms_g_9110") > 0

drop order_outcomes order_no

forvalues i = 2(2)`=_N' {
	replace outcome = "" in `i'
}

keep outcome OLS IV1 IV2 IV3

replace outcome = "$\Delta$ Mean Log Housing Rents" if strpos(outcome, "rentres") > 0
replace outcome = "$\Delta$ Log Total Number of Housing Units" if strpos(outcome, "houses") > 0
replace outcome = "$\Delta$ Log Total Number of Rooms" if strpos(outcome, "rooms") > 0

insobs 1, before(1)
insobs 1, before(8)
insobs 1, before(15)
insobs 1, before(22)
replace outcome = "\cmidrule{2-5} \textbf{Panel A: Penurious housing}" in 1
replace outcome = "\textbf{Panel B: Low-quality housing}" in 8
replace outcome = "\textbf{Panel C: Quality housing}" in 15
replace outcome = "\textbf{Panel D: Among recent migrants}" in 22

egen latex = concat(*), p("&")
replace latex = latex + "\\"
keep latex

insobs 2, before(1)
replace latex = "\toprule & OLS & IV & IV & IV \\" in 1
replace latex = "& (1) & (2) & (3) & (4) \\" in 2

insobs 5, after(30)
replace latex = "\midrule Macroregion fixed effects & Yes & Yes & Yes & Yes \\" in 31
replace latex = "Main controls set & Yes & No & Yes & Yes \\" in 32
replace latex = "Log population control & Yes & No & No & Yes \\" in 33
replace latex = "\bottomrule & & & & \\" in 34
replace latex = "\end{tabular}" in 35

insobs 1, before(1)
replace latex = "\begin{tabular}{lcccc}" in 1

cap file close myfile
file open myfile using "$da_out/tables/table_6.tex", write replace

forv i = 1/`=_N'{
	loc t0 = latex in `i'
	file write myfile `"`t0'"' _n 
} 

file close myfile


/* Table 7: Robustness 
*******************************************************************************/
* Baseline results, controlling for pop. growth, and controlling for com. time
** Housing outcomes
use "$da_out/results_2.dta", replace
keep outcome IV3 IV4 IV5 IV6 order_outcomes order_no
rename IV3 IV1
rename IV4 IV2
rename IV5 IV3
rename IV6 IV6
keep if strpos(outcome, "rentres2_r_9110") > 0 | strpos(outcome, "houses_r_9110") > 0 | strpos(outcome, "rooms_r_9110") > 0
tempfile aux1
save `aux1', replace 

** Labor outcomes
use "$da_out/results_1.dta", replace	
keep outcome IV3 IV4 IV5 IV6 order_outcomes order_no
rename IV3 IV1
rename IV4 IV2
rename IV5 IV3
rename IV6 IV6
keep if strpos(outcome, "wageres2_r_9110") > 0 | strpos(outcome, "emp_r_9110") > 0

append using `aux1'
gen order_new = _n
tempfile aux2
save `aux2', replace 

* Abs value SPEI
** Housing outcomes
use "$da_out/results_2_w.dta", replace	
keep outcome IV3 order_outcomes order_no
rename IV3 IV5
keep if strpos(outcome, "rentres2_r_9110") > 0 | strpos(outcome, "houses_r_9110") > 0 | strpos(outcome, "rooms_r_9110") > 0
tempfile aux3
save `aux3', replace 

** Labor outcomes
use "$da_out/results_1_w.dta", replace	
keep outcome IV3 order_outcomes order_no
rename IV3 IV5
keep if strpos(outcome, "wageres2_r_9110") > 0 | strpos(outcome, "emp_r_9110") > 0

append using `aux3'
gen order_new = _n
tempfile aux4
save `aux4', replace 

* Clustering at the mesoregion level
** Housing outcomes
use "$da_out/results_2_meso.dta", replace	
keep outcome IV3 order_outcomes order_no
rename IV3 IV4
keep if strpos(outcome, "rentres2_r_9110") > 0 | strpos(outcome, "houses_r_9110") > 0 | strpos(outcome, "rooms_r_9110") > 0
tempfile aux5
save `aux5', replace 

** Labor outcomes
use "$da_out/results_1_meso.dta", replace	
keep outcome IV3 order_outcomes order_no
rename IV3 IV4
keep if strpos(outcome, "wageres2_r_9110") > 0 | strpos(outcome, "emp_r_9110") > 0

append using `aux5'
gen order_new = _n

* Merge
merge 1:1 order_new using `aux2'
drop _merge

merge 1:1 order_new using `aux4'
drop _merge

order outcome IV1 IV2 IV3 IV4 IV5 IV6
drop order_outcomes order_no order_new

forvalues i = 2(2)`=_N' {
	replace outcome = "" in `i'
}

replace outcome = "$\Delta$ Mean Log Wages" if strpos(outcome, "wageres2_r_9110") > 0
replace outcome = "$\Delta$ Log Total Employment" if strpos(outcome, "emp_r_9110") > 0
replace outcome = "$\Delta$ Mean Log Housing Rents" if strpos(outcome, "rentres2_r_9110") > 0
replace outcome = "$\Delta$ Log Total Number of Housing Units" if strpos(outcome, "houses_r_9110") > 0
replace outcome = "$\Delta$ Log Total Number of Rooms" if strpos(outcome, "rooms_r_9110") > 0

insobs 1, before(1)
replace outcome = "\cmidrule{2-7} \textbf{Panel A: Labor Market outcomes}" in 1
insobs 1, before(6)
replace outcome = "\textbf{Panel B: Housing Market outcomes}" in 6

egen latex = concat(*), p("&")
replace latex = latex + "\\"
keep latex

insobs 3, before(1)
replace latex = "\toprule & Baseline & Population growth & Commuting time & Clustered SE & Absolute value of & Lagged shocks \\" in 1
replace latex = "& results & control & control & Mesoregion & SPEI as instrument & control \\" in 2
replace latex = "& (1) & (2) & (3) & (4) & (5) & (6) \\" in 3

insobs 5, after(15)
replace latex = "\midrule Macroregion fixed effects & Yes & Yes & Yes & Yes & Yes & Yes \\" in 16
replace latex = "Main controls set & Yes & Yes & Yes & Yes & Yes & Yes \\" in 17
replace latex = "Log population control & Yes & Yes & Yes & Yes & Yes & Yes \\" in 18
replace latex = "\bottomrule & & & & & & \\" in 19
replace latex = "\end{tabular}" in 20

insobs 1, before(1)
replace latex = "\begin{tabular}{lcccccc}" in 1

cap file close myfile
file open myfile using "$da_out/tables/table_7.tex", write replace

forv i = 1/`=_N'{
	loc t0 = latex in `i'
	file write myfile `"`t0'"' _n 
} 

file close myfile

/* Table 8: Robustness - Combes et al 2 steps procedure
*******************************************************************************/
use "$da_out/results_3.dta", replace

drop order_outcomes order_no

forvalues i = 2(2)`=_N' {
	replace outcome = "" in `i'
}

keep outcome OLS IV1 IV2 IV3

insobs 1, before(1)
insobs 1, before(14)
replace outcome = "\cmidrule{2-5} \textbf{Panel A: Wages}" in 1
replace outcome = "\textbf{Panel B: Housing Rents}" in 14

insobs 1, before(4)
replace outcome = "\textit{Services}" in 4
insobs 1, before(7)
replace outcome = "\textit{Manufacturing}" in 7
insobs 1, before(10)
replace outcome = "\textit{Less than high school}" in 10
insobs 1, before(13)
replace outcome = "\textit{High school or more}" in 13
insobs 1, before(16)
replace outcome = "\textit{Among recent migrants}" in 16
insobs 1, before(22)
replace outcome = "\textit{Penurious housing}" in 22
insobs 1, before(25)
replace outcome = "\textit{Low-quality housing}" in 25
insobs 1, before(28)
replace outcome = "\textit{Quality housing}" in 28
insobs 1, before(31)
replace outcome = "\textit{Among recent migrants}" in 31

replace outcome = "$\Delta$ City Fixed Effect (Log Wages Equation)" if strpos(outcome, "l_wage_r_9110") > 0
replace outcome = "~~~~~$\Delta$ City Fixed Effect (Log Wages Equation)" if strpos(outcome, "l_wage_r") > 0
replace outcome = "~~~~~$\Delta$ City Fixed Effect (Log Wages Equation)" if strpos(outcome, "l_wage_g") > 0
replace outcome = "$\Delta$ City Fixed Effect (Log Rents Equation)" if strpos(outcome, "l_rent_hh") > 0
replace outcome = "~~~~~$\Delta$ City Fixed Effect (Log Rents Equation)" if strpos(outcome, "l_rent") > 0

egen latex = concat(*), p("&")
replace latex = latex + "\\"
keep latex

insobs 2, before(1)
replace latex = "\toprule & OLS & IV & IV & IV \\" in 1
replace latex = "& (1) & (2) & (3) & (4) \\" in 2

insobs 5, after(35)
replace latex = "\midrule Macroregion fixed effects & Yes & Yes & Yes & Yes \\" in 36
replace latex = "Main controls set & Yes & No & Yes & Yes \\" in 37
replace latex = "Log population control & Yes & No & No & Yes \\" in 38
replace latex = "\bottomrule & & & & \\" in 39
replace latex = "\end{tabular}" in 40

insobs 1, before(1)
replace latex = "\begin{tabular}{lcccc}" in 1

cap file close myfile
file open myfile using "$da_out/tables/table_8.tex", write replace

forv i = 1/`=_N'{
	loc t0 = latex in `i'
	file write myfile `"`t0'"' _n 
} 

file close myfile

/* Table 9: other outcomes - labor market
*******************************************************************************/
use "$da_out/results_1.dta", replace

keep if strpos(outcome, "part") > 0 | strpos(outcome, "infor") > 0 | ///
strpos(outcome, "sh_r") > 0 | strpos(outcome, "d_hs") > 0

keep outcome OLS IV1 IV2 IV3 order_no order_outcomes
gen other = 1

tempfile part
save `part', replace   

* Expulsion Effect
use "$da_tmp/emigration_effect.dta", replace	

keep if strpos(outcome, "0010") > 0 

keep outcome OLS IV1 IV2 IV3 order_no order_outcomes
gen other = 3

* All together
append using `part'

replace other = 4 if strpos(outcome, "d_hs") > 0 | strpos(outcome, "inform") 

sort other order_outcomes order_no

replace outcome = "$\Delta$ Labor Force Participation Rate" if strpos(outcome, "part") > 0
replace outcome = "$\Delta$ Informality Rate (including self-employment)" if strpos(outcome, "inform_r") > 0
replace outcome = "$\Delta$ Informality Rate" if strpos(outcome, "inform2_r") > 0
replace outcome = "$\Delta$ Share of Employment in Manufacturing" if strpos(outcome, "imf") > 0
replace outcome = "$\Delta$ Share of Employment in Services" if strpos(outcome, "isv") > 0
replace outcome = "Emigration Rate 2000-2010" if strpos(outcome, "expulsion") > 0
replace outcome = "$\Delta$ Share with high school or more" if strpos(outcome, "d_hs") > 0

drop order_outcomes order_no other

forvalues i = 2(2)`=_N' {
	replace outcome = "" in `i'
}

insobs 1, before(1)
insobs 1, before(4)
insobs 1, before(9)
insobs 1, before(12)
insobs 1, before(15)
replace outcome = "\cmidrule{2-5} \textbf{Panel A: Participation rates}" in 1
replace outcome = "\textbf{Panel B: Participation in specific sectors}" in 4
replace outcome = "\textbf{Panel C: Emigration effect}" in 9
replace outcome = "\textbf{Panel D: Share of workers with high-school}" in 12
replace outcome = "\textbf{Panel E: Informality rates}" in 15

egen latex = concat(*), p("&")
replace latex = latex + "\\"
keep latex

insobs 2, before(1)
replace latex = "\toprule & OLS & IV & IV & IV \\" in 1
replace latex = "& (1) & (2) & (3) & (4) \\" in 2

insobs 5, after(21)
replace latex = "\midrule Macroregion fixed effects & Yes & Yes & Yes & Yes \\" in 22
replace latex = "Main controls set & Yes & No & Yes & Yes \\" in 23
replace latex = "Log population control & Yes & No & No & Yes \\" in 24
replace latex = "\bottomrule & & & & \\" in 25
replace latex = "\end{tabular}" in 26

insobs 1, before(1)
replace latex = "\begin{tabular}{lcccc}" in 1

cap file close myfile
file open myfile using "$da_out/tables/table_9.tex", write replace

forv i = 1/`=_N'{
	loc t0 = latex in `i'
	file write myfile `"`t0'"' _n 
} 

file close myfile

rm "$da_tmp/emigration_effect.dta"

/* Table 10: other outcomes - housing market
*******************************************************************************/
use "$da_out/results_2.dta", replace

keep if order_outcomes > 15
drop if order_outcomes >= 24

keep outcome OLS IV1 IV2 IV3 order_no order_outcomes

forvalues i = 2(2)`=_N' {
	replace outcome = "" in `i'
}

insobs 1, before(1)
insobs 1, before(6)
replace outcome = "\cmidrule{2-5} \textbf{Panel A: All housing units}" in 1
replace outcome = "\textbf{Panel B: By housing quality}" in 6

insobs 1, before(7)
insobs 1, before(12)
insobs 1, before(17)
replace outcome = "\textit{Penurious housing}" in 7
replace outcome = "\textit{Low-quality housing}" in 12
replace outcome = "\textit{Quality housing}" in 17

replace outcome = "$\Delta$ Residents' and Migrants' Ownership Rate" if strpos(outcome, "own_all_9110") > 0
replace outcome = "$\Delta$ Residents' Ownership Rate" if strpos(outcome, "own_residents_9110") > 0
replace outcome = "~~~~~$\Delta$ Log Total Number of Housing Units" if strpos(outcome, "houses_") > 0
replace outcome = "~~~~~$\Delta$ Log Total Number of Rooms" if strpos(outcome, "rooms_") > 0
replace outcome = "~~~~~$\Delta$ Residents' and Migrants' Ownership Rate" if strpos(outcome, "own_all") > 0
replace outcome = "~~~~~$\Delta$ Residents' Ownership Rate" if strpos(outcome, "own_residents") > 0

drop order_no order_outcomes

egen latex = concat(*), p("&")
replace latex = latex + "\\"
keep latex

insobs 2, before(1)
replace latex = "\toprule & OLS & IV & IV & IV \\" in 1
replace latex = "& (1) & (2) & (3) & (4) \\" in 2

insobs 5, after(23)
replace latex = "\midrule Macroregion fixed effects & Yes & Yes & Yes & Yes \\" in 24
replace latex = "Main controls set & Yes & No & Yes & Yes \\" in 25
replace latex = "Log population control & Yes & No & No & Yes \\" in 26
replace latex = "\bottomrule & & & & \\" in 27
replace latex = "\end{tabular}" in 28

insobs 1, before(1)
replace latex = "\begin{tabular}{lcccc}" in 1

cap file close myfile
file open myfile using "$da_out/tables/table_10.tex", write replace

forv i = 1/`=_N'{
	loc t0 = latex in `i'
	file write myfile `"`t0'"' _n 
} 

file close myfile

/* Appendix Table 3: Housing Market Outcomes for everyone 
*******************************************************************************/
use "$da_out/results_2.dta", replace

drop if order_outcomes <= 23 | order_outcomes >= 36

drop order_outcomes order_no

forvalues i = 2(2)`=_N' {
	replace outcome = "" in `i'
}

keep outcome OLS IV1 IV2 IV3

replace outcome = "$\Delta$ Mean Log Housing Rents" if strpos(outcome, "rentres2_9110") > 0
replace outcome = "$\Delta$ Log Total Number of Housing Units" if strpos(outcome, "houses_9110") > 0
replace outcome = "$\Delta$ Log Total Number of Rooms" if strpos(outcome, "rooms_9110") > 0
replace outcome = "~~~~~$\Delta$ Mean Log Housing Rents" if strpos(outcome, "rentres") > 0
replace outcome = "~~~~~$\Delta$ Log Total Number of Housing Units" if strpos(outcome, "houses") > 0
replace outcome = "~~~~~$\Delta$ Log Total Number of Rooms" if strpos(outcome, "rooms") > 0

insobs 1, before(1)
insobs 1, before(8)
insobs 1, before(9)
insobs 1, before(16)
insobs 1, before(23)
replace outcome = "\cmidrule{2-5} \textbf{Panel A: All housing units}" in 1
replace outcome = "\textbf{Panel B: By housing quality}" in 8
replace outcome = "\textit{Penurious housing}" in 9
replace outcome = "\textit{Low-quality housing}" in 16
replace outcome = "\textit{Quality housing}" in 23

egen latex = concat(*), p("&")
replace latex = latex + "\\"
keep latex

insobs 2, before(1)
replace latex = "\toprule & OLS & IV & IV & IV \\" in 1
replace latex = "& (1) & (2) & (3) & (4) \\" in 2

insobs 5, after(31)
replace latex = "\midrule Macroregion fixed effects & Yes & Yes & Yes & Yes \\" in 32
replace latex = "Main controls set & Yes & No & Yes & Yes \\" in 33
replace latex = "Log population control & Yes & No & No & Yes \\" in 34
replace latex = "\bottomrule & & & & \\" in 35
replace latex = "\end{tabular}" in 36

insobs 1, before(1)
replace latex = "\begin{tabular}{lcccc}" in 1

cap file close myfile
file open myfile using "$da_out/tables/appendix_table_3.tex", write replace

forv i = 1/`=_N'{
	loc t0 = latex in `i'
	file write myfile `"`t0'"' _n 
} 

file close myfile

/* Appendix Table 4: Housing Market Outcomes for recent migrants by housing quality
************************************************************************************/
use "$da_out/results_2.dta", replace

keep if strpos(outcome, "l_g_9110") > 0 | strpos(outcome, "m_g_9110") > 0 | ///
strpos(outcome, "h_g_9110") > 0 

drop order_outcomes order_no

forvalues i = 2(2)`=_N' {
	replace outcome = "" in `i'
}

keep outcome OLS IV1 IV2 IV3

replace outcome = "$\Delta$ Mean Log Housing Rents" if strpos(outcome, "rentres") > 0
replace outcome = "$\Delta$ Log Total Number of Housing Units" if strpos(outcome, "houses") > 0
replace outcome = "$\Delta$ Log Total Number of Rooms" if strpos(outcome, "rooms") > 0

insobs 1, before(1)
insobs 1, before(8)
insobs 1, before(15)
replace outcome = "\cmidrule{2-5} \textbf{Panel A: Penurious housing}" in 1
replace outcome = "\textbf{Panel B: Low-quality housing}" in 8
replace outcome = "\textbf{Panel C: Quality housing}" in 15

egen latex = concat(*), p("&")
replace latex = latex + "\\"
keep latex

insobs 2, before(1)
replace latex = "\toprule & OLS & IV & IV & IV \\" in 1
replace latex = "& (1) & (2) & (3) & (4) \\" in 2

insobs 5, after(23)
replace latex = "\midrule Macroregion fixed effects & Yes & Yes & Yes & Yes \\" in 24
replace latex = "Main controls set & Yes & No & Yes & Yes \\" in 25
replace latex = "Log population control & Yes & No & No & Yes \\" in 26
replace latex = "\bottomrule & & & & \\" in 27
replace latex = "\end{tabular}" in 28

insobs 1, before(1)
replace latex = "\begin{tabular}{lcccc}" in 1

cap file close myfile
file open myfile using "$da_out/tables/appendix_table_4.tex", write replace

forv i = 1/`=_N'{
	loc t0 = latex in `i'
	file write myfile `"`t0'"' _n 
} 

file close myfile

/* Appendix Table 5: Robustness - Heterogeneity - Labor Market Outcomes
*******************************************************************************/
* Baseline results, controlling for pop. growth, controlling for com. time, and lagged shocks
** By industry
use "$da_out/results_1.dta", replace	
keep outcome IV3 IV4 IV5 IV6 order_outcomes order_no
rename IV3 IV1
rename IV4 IV2
rename IV5 IV3
rename IV6 IV6

drop if strpos(outcome, "wageres2_r_9110") > 0 | ///
strpos(outcome, "emp_r_9110") > 0 | strpos(outcome, "part") > 0 | ///
order_outcomes > 12

sort order_outcomes order_no
gen order_new = _n
drop order_outcomes order_no
tempfile first_cols
save `first_cols', replace 

* Abs value SPEI
use "$da_out/results_1_w.dta", replace	
keep outcome IV3 order_outcomes order_no
rename IV3 IV5

drop if strpos(outcome, "wageres2_r_9110") > 0 | ///
strpos(outcome, "emp_r_9110") > 0 | strpos(outcome, "part") > 0

sort order_outcomes order_no
gen order_new = _n
drop order_outcomes order_no
tempfile spei
save `spei', replace

* Clustering at the mesoregion level
use "$da_out/results_1_meso.dta", replace	
keep outcome IV3 order_outcomes order_no
rename IV3 IV4

drop if strpos(outcome, "wageres2_r_9110") > 0 | ///
strpos(outcome, "emp_r_9110") > 0 | strpos(outcome, "part") > 0

sort order_outcomes order_no
gen order_new = _n
drop order_outcomes order_no

* Merge
merge 1:1 order_new using `first_cols'
drop _merge

merge 1:1 order_new using `spei'
drop _merge

order outcome IV1 IV2 IV3 IV4 IV5 IV6
drop order_new

forvalues i = 2(2)`=_N' {
	replace outcome = "" in `i'
}

replace outcome = "~~~~~$\Delta$ Mean Log Wages" if strpos(outcome, "wageres2_r") > 0
replace outcome = "~~~~~$\Delta$ Log Total Employment" if strpos(outcome, "emp_r") > 0
replace outcome = "$\Delta$ Mean Log Wages" if strpos(outcome, "wageres2_g") > 0
replace outcome = "$\Delta$ Log Total Employment" if strpos(outcome, "emp_g") > 0

insobs 1, before(1)
insobs 1, before(10)
insobs 1, before(19)
replace outcome = "\cmidrule{2-7} \textbf{Panel A: By industry}" in 1
replace outcome = "\textbf{Panel B: By schooling attainment}" in 10
replace outcome = "\textbf{Panel C: Among recent migrants}" in 19

insobs 1, before(2)
insobs 1, before(7)
insobs 1, before(13)
insobs 1, before(18)
replace outcome = "\textit{Services}" in 2
replace outcome = "\textit{Manufacturing}" in 7
replace outcome = "\textit{Less than high school}" in 13
replace outcome = "\textit{High school or more}" in 18

egen latex = concat(*), p("&")
replace latex = latex + "\\"
keep latex

insobs 3, before(1)
replace latex = "\toprule & Baseline & Population growth & Commuting time & Clustered SE & Absolute value of & Lagged shocks \\" in 1
replace latex = "& results & control & control & Mesoregion & SPEI as instrument & control \\" in 2
replace latex = "& (1) & (2) & (3) & (4) & (5) & (6) \\" in 3

insobs 5, after(30)
replace latex = "\midrule Macroregion fixed effects & Yes & Yes & Yes & Yes & Yes & Yes \\" in 31
replace latex = "Main controls set & Yes & Yes & Yes & Yes & Yes & Yes \\" in 32
replace latex = "Log population control & Yes & Yes & Yes & Yes & Yes & Yes \\" in 33
replace latex = "\bottomrule & & & & & & \\" in 34
replace latex = "\end{tabular}" in 35

insobs 1, before(1)
replace latex = "\begin{tabular}{lcccccc}" in 1

cap file close myfile
file open myfile using "$da_out/tables/appendix_table_5.tex", write replace

forv i = 1/`=_N'{
	loc t0 = latex in `i'
	file write myfile `"`t0'"' _n 
} 

file close myfile

/* Appendix Table 6: Robustness - Heterogeneity - Housing Market Outcomes
*******************************************************************************/
* Baseline results, controlling for pop. growth, controlling for com. time, and lagged shocks
** By housing quality and among residents
use "$da_out/results_2.dta", replace	
keep outcome IV3 IV4 IV5 IV6 order_outcomes order_no
rename IV3 IV1
rename IV4 IV2
rename IV5 IV3
rename IV6 IV6

keep if strpos(outcome, "l_r_9110") > 0 | strpos(outcome, "m_r_9110") > 0 | ///
strpos(outcome, "h_r_9110") > 0 | strpos(outcome, "rentres2_g_9110") > 0 | ///
strpos(outcome, "houses_g_9110") > 0 | strpos(outcome, "rooms_g_9110") > 0

gen aux = strpos(outcome, "_g_") > 0
replace IV3 = "" if aux == 0
drop aux

sort order_outcomes order_no
gen order_new = _n
drop order_outcomes order_no

tempfile first_cols
save `first_cols', replace   

* Abs value SPEI
use "$da_out/results_2_w.dta", replace	
keep outcome IV3 order_outcomes order_no
rename IV3 IV5

keep if strpos(outcome, "l_r_9110") > 0 | strpos(outcome, "m_r_9110") > 0 | ///
strpos(outcome, "h_r_9110") > 0 | strpos(outcome, "rentres2_g_9110") > 0 | ///
strpos(outcome, "houses_g_9110") > 0 | strpos(outcome, "rooms_g_9110") > 0

sort order_outcomes order_no
gen order_new = _n
drop order_outcomes order_no

tempfile spei
save `spei', replace

* Clustering at the mesoregion level
use "$da_out/results_2_meso.dta", replace	
keep outcome IV3 order_outcomes order_no
rename IV3 IV4

keep if strpos(outcome, "l_r_9110") > 0 | strpos(outcome, "m_r_9110") > 0 | ///
strpos(outcome, "h_r_9110") > 0 | strpos(outcome, "rentres2_g_9110") > 0 | ///
strpos(outcome, "houses_g_9110") > 0 | strpos(outcome, "rooms_g_9110") > 0

sort order_outcomes order_no
gen order_new = _n
drop order_outcomes order_no

* Merge
merge 1:1 order_new using `first_cols'
drop _merge

merge 1:1 order_new using `spei'
drop _merge

order outcome IV1 IV2 IV3 IV4 IV5 IV6

* Merge commuting time results by housing quality
merge 1:1 order_new using "$da_tmp/results_comtime.dta"
drop _merge

replace IV3 = IV_aux if order_new < 19

sort order_new
drop order_new IV_aux
order outcome IV1 IV2 IV3 IV4 IV5 IV6

forvalues i = 2(2)`=_N' {
	replace outcome = "" in `i'
}

replace outcome = "$\Delta$ Mean Log Housing Rents" if strpos(outcome, "rent") > 0
replace outcome = "$\Delta$ Log Total Number of Housing Units" if strpos(outcome, "houses") > 0
replace outcome = "$\Delta$ Log Total Number of Rooms" if strpos(outcome, "rooms") > 0

insobs 1, before(1)
insobs 1, before(8)
insobs 1, before(15)
insobs 1, before(22)
replace outcome = "\cmidrule{2-7} \textbf{Panel A: Penurious housing}" in 1
replace outcome = "\textbf{Panel B: Low-quality housing}" in 8
replace outcome = "\textbf{Panel C: Quality housing}" in 15
replace outcome = "\textbf{Panel D: Among recent migrants}" in 22

egen latex = concat(*), p("&")
replace latex = latex + "\\"
keep latex

insobs 3, before(1)
replace latex = "\toprule & Baseline & Population growth & Commuting time & Clustered SE & Absolute value of & Lagged shocks \\" in 1
replace latex = "& results & control & control & Mesoregion & SPEI as instrument & control \\" in 2
replace latex = "& (1) & (2) & (3) & (4) & (5) & (6) \\" in 3

insobs 5, after(31)
replace latex = "\midrule Macroregion fixed effects & Yes & Yes & Yes & Yes & Yes & Yes \\" in 32
replace latex = "Main controls set & Yes & Yes & Yes & Yes & Yes & Yes \\" in 33
replace latex = "Log population control & Yes & Yes & Yes & Yes & Yes & Yes \\" in 34
replace latex = "\bottomrule & & & & & & \\" in 35
replace latex = "\end{tabular}" in 36

insobs 1, before(1)
replace latex = "\begin{tabular}{lcccccc}" in 1

cap file close myfile
file open myfile using "$da_out/tables/appendix_table_6.tex", write replace

forv i = 1/`=_N'{
	loc t0 = latex in `i'
	file write myfile `"`t0'"' _n 
} 

file close myfile

rm "$da_tmp/results_comtime.dta"

/* Appendix Table 7: Effects of Weather Shocks on Migration (using the absolute value of the SPEI measure)
***********************************************************************************************************/
use "$da_tmp/first_stage_rural_abs.dta", replace
append using "$da_tmp/first_stage_immig_abs.dta"

drop var

gen s1 = "*" if c2 < 0.1
replace s1 = "**" if c2 < 0.05
replace s1 = "***" if c2 < 0.01
gen coef1 = string(c1, "%20.3fc")
replace coef1 = string(c1, "%20.0fc") if _n == 3 | _n == 8
replace coef1 = coef1 + s1 if _n == 1 | _n == 5
replace coef1 = "(" + coef1 + ")" if _n == 2 | _n == 6
replace coef1 = string(c1, "%20.2fc") if _n == 7

gen s2 = "*" if c4 < 0.1
replace s2 = "**" if c4 < 0.05
replace s2 = "***" if c4 < 0.01
gen coef2 = string(c3, "%20.3fc")
replace coef2 = string(c3, "%20.0fc") if _n == 3 | _n == 8
replace coef2 = coef2 + s2 if _n == 1 | _n == 5
replace coef2 = "(" + coef2 + ")" if _n == 2 | _n == 6
replace coef2 = string(c3, "%20.2fc") if _n == 7

gen s3 = "*" if c6 < 0.1
replace s3 = "**" if c6 < 0.05
replace s3 = "***" if c6 < 0.01
gen coef3 = string(c5, "%20.3fc")
replace coef3 = string(c5, "%20.0fc") if _n == 3 | _n == 8
replace coef3 = coef3 + s3 if _n == 1 | _n == 5
replace coef3 = "(" + coef3 + ")" if _n == 2 | _n == 6
replace coef3 = string(c5, "%20.2fc") if _n == 7

keep coef*

gen outcome = ""
replace outcome = "Observations" if _n == 3 | _n == 8
replace outcome = "Average of dependent variable" if _n == 4 | _n == 9
replace outcome = "F statistic" if _n == 7
replace outcome = "\cmidrule{2-4} Average Absolute Value SPEI" if _n == 1
replace outcome = "\cmidrule{2-4}SPEI-based shift-share IV" if _n == 5
order outcome

insobs 1, before(5)
insobs 1, before(3)
insobs 1, before(9)

egen latex = concat(*), p("&")
replace latex = latex + "\\"
keep latex

insobs 4, before(1)
replace latex = "\begin{tabular}{lccc}" in 1
replace latex = "\toprule & (1) & \multicolumn{1}{c}{(2)} & \multicolumn{1}{c}{(3)} \\" in 2
replace latex = "\multicolumn{4}{l}{\textbf{Panel A: Effects of precipitations on rural emigration }} \\" in 3
replace latex = "& \multicolumn{3}{c}{\textit{Emigration rate from rural areas}} \\" in 4
insobs 6, before(11)
replace latex = "Macrorregion fixed effects & Yes & \multicolumn{1}{c}{Yes} & \multicolumn{1}{c}{Yes} \\" in 11
replace latex = "Main controls set & No & \multicolumn{1}{c}{Yes} & \multicolumn{1}{c}{Yes} \\" in 12
replace latex = "Log population control & No & \multicolumn{1}{c}{No} & \multicolumn{1}{c}{Yes} \\" in 13
replace latex = "&&& \\" in 14
replace latex = "\multicolumn{4}{l}{\textbf{Panel B: Effect of weather-based IV on rural immigration }} \\" in 15
replace latex = "& \multicolumn{3}{c}{\textit{Rural immigration rate to cities}} \\" in 16
insobs 6, after(22)
replace latex = "&&& \\" in 23
replace latex = "Macrorregion fixed effects & Yes & \multicolumn{1}{c}{Yes} & \multicolumn{1}{c}{Yes} \\" in 24
replace latex = "Main controls set & No & \multicolumn{1}{c}{Yes} & \multicolumn{1}{c}{Yes} \\" in 25
replace latex = "Log population control & No & \multicolumn{1}{c}{No} & \multicolumn{1}{c}{Yes} \\" in 26
replace latex = "\bottomrule & & & \\" in 27
replace latex = "\end{tabular}" in 28

cap file close myfile
file open myfile using "$da_out/tables/appendix_table_7.tex", write replace

forv i = 1/`=_N'{
	loc t0 = latex in `i'
	file write myfile `"`t0'"' _n 
} 

file close myfile

/* Appendix Table B1: Labor Market Outcomes 2000-2010
*******************************************************************************/
use "$da_out/results_4.dta", replace

keep outcome OLS IV1 IV2 IV3 order_no order_outcomes

sort order_outcomes order_no

replace outcome = "$\Delta$ Mean Log Wages" if strpos(outcome, "wageres2_r_0010") > 0
replace outcome = "$\Delta$ Log Total Employment" if strpos(outcome, "emp_r_0010") > 0
replace outcome = "~~~~~$\Delta$ Mean Log Wages" if strpos(outcome, "wageres2_r") > 0
replace outcome = "~~~~~$\Delta$ Log Total Employment" if strpos(outcome, "emp_r") > 0
replace outcome = "$\Delta$ Mean Log Wages" if strpos(outcome, "wageres2_g") > 0
replace outcome = "$\Delta$ Log Total Employment" if strpos(outcome, "emp_g") > 0

drop order_outcomes order_no 

forvalues i = 2(2)`=_N' {
	replace outcome = "" in `i'
}

insobs 1, before(1)
insobs 1, before(6)
insobs 1, before(15)
insobs 1, before(24)
replace outcome = "\cmidrule{2-5} \textbf{Panel A: All residents}" in 1
replace outcome = "\textbf{Panel B: By industry}" in 6
replace outcome = "\textbf{Panel C: By schooling attainment}" in 15
replace outcome = "\textbf{Panel D: Among recent migrants}" in 24

insobs 1, before(7)
insobs 1, before(12)
insobs 1, before(18)
insobs 1, before(23)
replace outcome = "\textit{Services}" in 7
replace outcome = "\textit{Manufacturing}" in 12
replace outcome = "\textit{Less than high school}" in 18
replace outcome = "\textit{High school or more}" in 23

egen latex = concat(*), p("&")
replace latex = latex + "\\"
keep latex

insobs 2, before(1)
replace latex = "\toprule & OLS & IV & IV & IV \\" in 1
replace latex = "& (1) & (2) & (3) & (4) \\" in 2

insobs 5, after(34)
replace latex = "\midrule Macroregion fixed effects & Yes & Yes & Yes & Yes \\" in 35
replace latex = "Main controls set & Yes & No & Yes & Yes \\" in 36
replace latex = "Log population control & Yes & No & No & Yes \\" in 37
replace latex = "\bottomrule & & & & \\" in 38
replace latex = "\end{tabular}" in 39

insobs 1, before(1)
replace latex = "\begin{tabular}{lcccc}" in 1

cap file close myfile
file open myfile using "$da_out/tables/appendix_table_B1.tex", write replace

forv i = 1/`=_N'{
	loc t0 = latex in `i'
	file write myfile `"`t0'"' _n 
} 

file close myfile

/*******************************************************************************
7. Deleting files
*******************************************************************************/
local files: dir "$da_tmp" files "*.ster"
foreach file in `files' {
    erase "$da_tmp/`file'"
}

local files: dir "$da_tmp" files "shockstransf*"
foreach file in `files' {
    erase "$da_tmp/`file'"
}

local files: dir "$da_out" files "results*"
foreach file in `files' {
    erase "$da_out/`file'"
}

